﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Prism.ViewModel;
using Microsoft.Practices.Prism;
using System.ComponentModel;
using Microsoft.Practices.Prism.Commands;
using BBInterfaceNET.Events.Commands;

namespace BBInterfaceNET.Designer.ViewModels
{
    public abstract class DocumentViewModel:NotificationObject, IActiveAware
    {
        #region fields
        private string filePath;
        private string projectRoot;
        private DelegateCommand<CancelEventArgs> shutdownCmd;
        private DelegateCommand closeCmd, saveCmd;

        public event EventHandler FileClosed = delegate { };
        #endregion

        public DocumentViewModel()
        {
            shutdownCmd = new DelegateCommand<CancelEventArgs>(ShutdownOverride);
            saveCmd = new DelegateCommand(OnSaveCommand, () => CanSaveOverride);

            InfrastructureCommands.ShutdownCommand.RegisterCommand(shutdownCmd);
            InfrastructureCommands.SaveAllCommand.RegisterCommand(saveCmd);
            InfrastructureCommands.SaveCommand.RegisterCommand(saveCmd);
        }

        #region properties
        public string ProjectRoot
        {
            get { return projectRoot; }
            set { projectRoot = value; }
        }
        public string FilePath
        {
            get { return filePath; }
            set
            {
                if (filePath != value)
                {
                    filePath = value;
                    RaisePropertyChanged(() => FilePath);
                }
            }
        }
        #endregion

        #region commands
        public DelegateCommand<CancelEventArgs> ShutdownCommand
        {
            get { return shutdownCmd; }
        }
        public DelegateCommand CloseCommand
        {
            get
            {
                if (closeCmd == null) closeCmd = new DelegateCommand(OnClose);
                return closeCmd;
            }
        }
        public DelegateCommand SaveCommand
        {
            get { return saveCmd; }
        }
        #endregion

        #region command handlers
        private void OnClose()
        {
            CancelEventArgs args = new CancelEventArgs();
            CloseOverride(args);

            if (!args.Cancel)
            {
                InfrastructureCommands.SaveAllCommand.UnregisterCommand(SaveCommand);
                InfrastructureCommands.SaveCommand.UnregisterCommand(SaveCommand);
                InfrastructureCommands.ShutdownCommand.UnregisterCommand(ShutdownCommand);
                FileClosed(this, EventArgs.Empty);
            }
        }

        private void OnSaveCommand()
        {
            if (CanSaveOverride)
                SaveOverride();
        }
        #endregion

        #region IActiveAware impl
        private bool isActive;
        public bool IsActive
        {
            get { return isActive; }
            set
            {
                if (isActive != value)
                {
                    isActive = value;
                    OnIsActiveChanged();
                }
            }
        }

        public event EventHandler IsActiveChanged;

        protected virtual void OnIsActiveChanged()
        {
            if (IsActiveChanged != null)
                IsActiveChanged(this, EventArgs.Empty);
        }
        #endregion

        #region virtual method definitions
        protected virtual void ShutdownOverride(CancelEventArgs args)
        { }
        protected virtual void CloseOverride(CancelEventArgs args)
        { }
        protected virtual void SaveOverride()
        { }
        protected virtual bool CanSaveOverride
        {
            get { return false; }
        }
        #endregion
    }
}
